"""
Copyright (c) 2023 - present SKbioinfo
"""

from apps.NGS_API.connector.db_common import query_info
from apps.NGS_API.charts.utils import define_month_time
from flask_restful import Resource
from flask import request
from flask_login import login_required

from apps.NGS_API.utils.log_helper import lg
from apps.NGS_API.charts.common_query import *
import datetime as dt

months = {
        1:"一月",
        2:"二月",
        3:"三月",
        4:"四月",
        5:"五月",
        6:"六月",
        7:"七月",
        8:"八月",
        9:"九月",
        10:"十月",
        11:"十一月",
        12:"十二月",
        0:"十二月",
        -1:"十一月"
}

class interfaceRecentAnaBar(Resource):
    def get(self):
        try:
            recent_ana = {}
            today = dt.datetime.today()
            last_month = today.month
            mid_month = last_month - 1
            first_month = mid_month - 1
            three_months=[months[first_month],months[mid_month],months[last_month]]
            recent_ana["months"] = three_months
            '''
                Recent bioinfo analysis: recent three months 
            '''
            ### initialization ###
            recent_ana["SK_mag"] = [0,0,0]
            recent_ana["tNGS"] = [0,0,0]
            ### define month time ###
            month_time = define_month_time()
            three_months_ana_sql=three_months_interval_group_query(
                name = "ana",
                table_name= "ams_ngs_analysis",
                month_time=month_time,
                col = "operate_end_time",
                group_col = "pipeline"
            )
            three_months_ana_results=query_info(three_months_ana_sql)
            for result in three_months_ana_results:
                recent_ana[result[0]]=[
                    int(result[3]),
                    int(result[2]),
                    int(result[1])
                ]
            return recent_ana
        except Exception as e:
            lg.error(e)

'''
    INTERFACE: montly report of bioanalysis
    DATA_URL: /api/charts/lineCharts/bioana
    TEMPLATE: /template/home/project/index_overview.html
    JAVASCRIPT: /static/interface/Charts/interfaceBioanaCharts.js
'''
class interfaceMontlyAnaLine(Resource):
    @login_required
    def get(self):
        try:
            recent_ana = {}
            ### Plot X axis titles: monthly analysis statistic ###
            recent_ana["time_point"] = ["0st-6th","6th-12th","12th-18th","18th-24th","24th-30th"]
            ### define month time ###
            month_time = define_month_time()
            ### one month ago ###
            one_month_ago = (dt.datetime.now() - dt.timedelta(days=30)).strftime("%Y-%m-%d")
            recent_ana["start_date"] = one_month_ago
            recent_ana["end_date"] = month_time
            ### SQL: monthly analysis query ###
            monthly_ana_sql = six_intervals_group_sql(
                name = "ana",
                table_name = "ams_ngs_analysis",
                col = "operate_end_time",
                month_time = month_time,
                group_col = "pipeline"
            )
            recent_ana["SK_mag"] = [0,0,0,0,0,0]
            recent_ana["CHO"] = [0,0,0,0,0,0]
            monthly_ana_results = query_info(monthly_ana_sql)
            for result in monthly_ana_results:
                ana = [ int(count) for count in result[1:6]]
                ana.reverse()
                ana.insert(0,0)
                recent_ana[result[0]]=ana
            return recent_ana    
        except Exception as e:
            lg.error(e)
            
'''
    INTERFACE: monthly report of samples
    DATA_URL: /api/charts/lineCharts/sample
    TEMPLATE: /template/home/laboratory/NGS/NGS_Samples_search.html
    JAVASCRIPT: /static/interface/interfaceSampleAmount.js
'''    
class interfaceAnaPie(Resource):
    @login_required
    def get(self):
        try:
            ana_total_sql = total_query_sql(
                col="analysis_id",
                table_name="ams_ngs_analysis"
            )
            ana_total_count={}
            ana_complete = 0
            ana_pending = 0
            tmp_pie={}
            anas = query_info(ana_total_sql)
            for ana in anas:
                tmp_pie[ana[0]] = {"status":1}
            for id,infos in tmp_pie.items():
                if infos["status"]==1:
                    ana_complete += 1
                elif infos["status"]==0:
                    ana_pending += 1 
            ana_total_count["anas"] = [ana_complete,ana_pending]
            return ana_total_count
        except Exception as e:
            lg.error(e)